package com.tiantian.quanminxiu.adapter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.R;

import java.util.List;

/**
 * Created by admin on 2016/9/9.
 */
public class BannerVPAdatper extends PagerAdapter implements ViewPager.OnPageChangeListener {

    private List<ImageView> views;
    private View[] dots;
    // 记录当前选中位置
    private int currentIndex = 0;
    private LinearLayout dotLayout;
    private ViewPager viewPager;
    private Activity activity;

    public BannerVPAdatper(Activity activity, List<ImageView> views, LinearLayout dotLayout,ViewPager viewPager) {
        this.activity = activity;
        this.views = views;
        this.dotLayout = dotLayout;
        this.viewPager = viewPager;
        initDots();
    }

    @Override
    public int getCount() {
        if (views == null)
            return 0;
        return views.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = views.get(position);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = (String) imageView.getTag();
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                System.out.println(position);
                Uri content_url = Uri.parse(url);
                intent.setData(content_url);
                activity.startActivity(intent);
            }
        });
        container.addView(imageView, 0);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }

    //region 滑动状态改变监听 onPageScrollStateChanged(int arg0)
    @Override
    public void onPageScrollStateChanged(int arg0) {
        viewPager.getParent().requestDisallowInterceptTouchEvent(true);
    }

    // 当当前页面被滑动时调用
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    // 当新的页面被选中时调用
    @Override
    public void onPageSelected(int arg0) {
        // 设置底部小点选中状态
        F_setCurrentDot(arg0);
    }
    //endregion

    //region 导航小点 initDots（）
    private void initDots() {
        dots = new View[views.size()];

        // 循环取得小点图片
        for (int i = 0; i < views.size(); i++) {
            View v = new View(activity);
            if(i==0){
                v.setBackgroundResource(R.drawable.shape_oval_orange_small);
            }else {
                v.setBackgroundResource(R.drawable.shape_oval_gray_samll);
            }
            dotLayout.addView(v);
//            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(CommonUtils.dip2px(activity,6),CommonUtils.dip2px(activity,6));
            params.rightMargin = CommonUtils.dip2px(activity,10);
            v.setLayoutParams(params);
            dots[i] = v;
        }
    }
    //endregion

    //region 设置图片位置 F_setCurrentDot(int position)
    private void F_setCurrentDot(int position) {
        if (position < 0 || position > views.size() - 1) {
            return;
        }
        dots[currentIndex].setBackgroundResource(R.drawable.shape_oval_gray_samll);
        dots[position].setBackgroundResource(R.drawable.shape_oval_orange_small);

        currentIndex = position;
    }
    //endregion
}
